<?php
	class Article_model extends CI_Model {
		public $table_name="article";
    public function get_by_page($page,$pagesize){
        $offset=($page-1)*$pagesize;
        $query=$this->db->order_by("id","desc")->limit($pagesize,$offset)->get($this->table_name);
        return $query->result();
    }
    public function count_all(){
        return $this->db->count_all_results($this->table_name);
    }

    public function get_all()
    {	
    	
        $articles = $this->db->get($this->table_name);
        return $articles->result();
    }
      public function get_one($id)
    {
        $article = $this->db->where("id",$id)->get($this->table_name);
        $a= $article->result();
        return $a[0];
    }
       public function get_by_catalog_id($catalog_id)
    {
        $article = $this->db->where("catalog_id",$catalog_id)->get($this->table_name);
        $a= $article->result();
        return $a;
    }
    	public function view_once($id)
    {
    	$this->db->query("update $this->table_name set `view`=`view`+1 where `id`=$id");
    }
        public function add_one($data)
    {   
        $data["create_time"]=time();
        $this->db->insert($this->table_name,$data);
    }
        public function recommend($id)
    {
        $data=$this->db->where("id",$id)->get($this->table_name);
        $d=$data->row();
        $recommend=$d->recommend;
        $recommend=$recommend==1?"0":"1";
        $data=array(
            "recommend"=>$recommend,
        );
        $this->db->where('id',$id)->update($this->table_name, $data);
    }
        public function edit_one($id,$data)
    {
        
         $this->db->where('id',$id)->update($this->table_name,$data);
    }
        public function delete_one($id)
    {
        $this->db->where('id',$id)->delete($this->table_name);
    }
}
    
